﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using TinyCinema.Bussiness;

namespace TinyCinema.Bussiness.Customer
{
    public class Type : System.Windows.DependencyObject
    {
        public static DependencyProperty CurrentViewProperty = DependencyProperty.Register("CurrentView", typeof(Data.CustomerType), typeof(Type));
        public Data.CustomerType CurrentView
        {
            get { return (Data.CustomerType)GetValue(CurrentViewProperty); }
            set
            {
                SetValue(CurrentViewProperty, value);
                if ((value as Data.CustomerType).ID == null) IsEditable = true;
                else IsEditable = false;
            }
        }
        public static DependencyProperty IsDetailViewingProperty = DependencyProperty.Register("IsDetailViewing", typeof(bool), typeof(Type));
        public bool IsDetailViewing
        {
            get { return (bool)GetValue(IsDetailViewingProperty); }
            set { SetValue(IsDetailViewingProperty, value); }
        }
        public static DependencyProperty lstCustomerTypeProperty = DependencyProperty.Register("lstCustomerType", typeof(ObservableCollection<Data.CustomerType>), typeof(GridView));
        public ObservableCollection<Data.CustomerType> lstCustomerType
        {
            get { return (ObservableCollection<Data.CustomerType>)GetValue(lstCustomerTypeProperty); }
            set { SetValue(lstCustomerTypeProperty, value); }
        }
        public static DependencyProperty IsEditableProperty = DependencyProperty.Register("IsEditable", typeof(bool), typeof(Type));
        public bool IsEditable
        {
            get { return (bool)GetValue(IsEditableProperty); }
            set
            {
                SetValue(IsEditableProperty, value);
            }
        }
        public static DependencyProperty IsViewModeEnableProperty = DependencyProperty.Register("IsViewModeEnable", typeof(bool), typeof(Type));
        public bool IsViewModeEnable
        {
            get { return (bool)GetValue(IsViewModeEnableProperty); }
            set
            {
                SetValue(IsViewModeEnableProperty, value);
            }
        }
        public ICommand AddCurrentViewCommand { get; set; }
        public ICommand CreateCommand { get; set; }
        public ICommand ChooseMenuCommand { get; set; }
        public ICommand EditCommand { get; set; }
        public ICommand BackCommand { get; set; }
        public ICommand SaveCommand { get; set; }
        public ICommand DeleteCommand { get; set; }
        public ICommand AddPhotoCommand { get; set; }
        public Messenger Messenger { get; set; }
        public Type()
        {
            Messenger = new Bussiness.Messenger();
            lstCustomerType = Data.CustomerType.getAllCustomerType();
            SetValue(IsViewModeEnableProperty, true);
            IsEditable = true;
            IsDetailViewing = false;
            ChooseMenuCommand = new RelayCommand<Data.CustomerType>(
                (s) => true,
                (s) =>
                {
                    CurrentView = s;
                    IsEditable = false;
                    IsViewModeEnable = false;
                    IsDetailViewing = true;
                }
            );
            CreateCommand = new RelayCommand<Data.CustomerType>(
                (s) => IsViewModeEnable,
                (s) =>
                {
                    CurrentView = new Data.CustomerType(false);
                    IsEditable = true;
                    IsViewModeEnable = false;
                    IsDetailViewing = true;

                }
            );
            EditCommand = new RelayCommand<Data.CustomerType>(
                (s) => !IsEditable,
                (s) =>
                {
                    IsEditable = true;
                }
            );
            SaveCommand = new RelayCommand<Data.CustomerType>(
                (s) => true,
                (s) =>
                {
                    if (lstCustomerType.IndexOf(CurrentView) == -1) lstCustomerType.Add(CurrentView);
                    CurrentView.Save();
                    IsEditable = false;
                    IsDetailViewing = false;
                    IsViewModeEnable = true;
                }
            );
            DeleteCommand = new RelayCommand<Data.CustomerType>(
                (s) => !IsViewModeEnable || !IsEditable,
                (s) =>
                {
                    var message1 = new Message.DeleteConfirmation();
                    Messenger.Send(message1);
                    if (message1.Result == MessageBoxResult.Yes)
                        try
                        {
                            CurrentView.Delete();
                            lstCustomerType.Remove(CurrentView);
                            IsViewModeEnable = true;
                            IsEditable = true;
                            IsDetailViewing = false;
                        }
                        catch
                        {
                            var message2 = new Message.DeleteFailed();
                            Data.Execution.Restore();
                        }

                }
            );
            BackCommand = new RelayCommand<Data.CustomerType>(
                (s) => true,
                (s) =>
                {
                    if (IsEditable)
                    {
                        var message1 = new Message.BackConfimation();
                        Messenger.Send(message1);
                        if (message1.Result == MessageBoxResult.Yes)
                        {
                            Messenger.Send(new Message.BackConfimationOK());
                            IsViewModeEnable = true;
                            IsDetailViewing = false;
                            IsEditable = true;
                        }
                    }
                    else
                    {

                        Messenger.Send(new Message.BackConfimationOK());
                        IsViewModeEnable = true;
                        IsDetailViewing = false;
                        IsEditable = true;
                    }

                }
            );
            AddPhotoCommand = new RelayCommand<Object>(
                (s) => true,
                (s) =>
                {
                    CurrentView.AddPhoto();

                }
            );
        }
    }
}
